<#include "macro_.ftl">
package ${packageCode}.${moduleMapperKey}.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.fand.dto.PaginationDto;
import ${packageCode}.${moduleMapperKey}.dao.${businessCode}Dao;
<#list poMapperList as poMapper>
<#if poMapper.markService>
import ${packageCode}.${moduleMapperKey}.dto.${poMapper.code}Dto;
</#if>
</#list>

/**
 * ${businessMapper.name!businessCode}服务
 *
 * @author ${author!'yaoHT'}
 * 
 */
@Getter 
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ${businessCode}DefaultService implements ${businessCode}Service {
	private ${businessCode}Dao ${businessCode?uncap_first}Dao;
	
	@Override
	@CacheEvict(value = ${businessCode}Service.CACHE_NAME, allEntries = true)
	public void reload() {
	}
<#if businessCode == (moduleMapperKey?cap_first)>
<#list moduleMapper.businessMap?values as localBusiness>
<#if (localBusiness.code!) != (moduleMapperKey?cap_first)>

	private ${localBusiness.code!}Service ${localBusiness.code?uncap_first}Service;
</#if>
</#list>
</#if>
<#list poMapperList as poMapper>
<#if poMapper.markService>

	@Override
	@Transactional
	public ${poMapper.code}Dto save${poMapper.code}ByIdOrCodeFor${poMapper.code}(${poMapper.code}Dto save${poMapper.code}) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(save${poMapper.code}.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		${poMapper.code}Dto old${poMapper.code} = this.${businessCode?uncap_first}Dao.select${poMapper.code}By1ForMappedObject(${poMapper.code}Dto.class,
				save${poMapper.code}, null, whereEqProperties);

		if (old${poMapper.code} == null) {
			this.${businessCode?uncap_first}Dao.insert${poMapper.code}By1(save${poMapper.code});

			return save${poMapper.code};
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(save${poMapper.code}, old${poMapper.code}, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.${businessCode?uncap_first}Dao.update${poMapper.code}By1(save${poMapper.code}, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(save${poMapper.code}, old${poMapper.code}, updateProperties);
		}

		return old${poMapper.code};
	}

	@Override
	@Transactional
	public ${poMapper.code}Dto save${poMapper.code}By2For${poMapper.code}(${poMapper.code}Dto save${poMapper.code}) {
		String[] whereEqProperties = null;

//		if (StringUtils.isNotBlank(save${poMapper.code}.getId())) {
//			whereEqProperties = new String[] { "id" };
//		} else {
//			whereEqProperties = new String[] { "code" };
//		}
	
		${poMapper.code}Dto old${poMapper.code} = this.${businessCode?uncap_first}Dao.select${poMapper.code}By1ForMappedObject(${poMapper.code}Dto.class,
				save${poMapper.code}, null, whereEqProperties);

		if (old${poMapper.code} == null) {
			this.${businessCode?uncap_first}Dao.insert${poMapper.code}By1(save${poMapper.code}, new String[] {});

			return save${poMapper.code};
		}
		
		String[] updateProperties = com.fand.utils.BeanUtils.differProperties(save${poMapper.code}, old${poMapper.code}, "code", "name");

		if (ArrayUtils.isNotEmpty(updateProperties)) {
			this.${businessCode?uncap_first}Dao.update${poMapper.code}By1(save${poMapper.code}, updateProperties, whereEqProperties);

			com.fand.utils.BeanUtils.copyProperties(save${poMapper.code}, old${poMapper.code}, updateProperties);
		}

		return old${poMapper.code};
	}

	@Override
	@Transactional
	public int delete${poMapper.code}By1(${poMapper.code}Dto delete${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.delete${poMapper.code}By1(delete${poMapper.code}, new String[] {});
	}

	@Override
	@Transactional
	public int delete${poMapper.code}ByIds(${poMapper.code}Dto delete${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.delete${poMapper.code}ByIds(delete${poMapper.code});
	}

	@Override
	@Transactional
	public int set${poMapper.code}By1(${poMapper.code}Dto set${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.update${poMapper.code}By1(set${poMapper.code}, new String[] {}, new String[] {});
	}

	@Override
	@Transactional
	public int set${poMapper.code}By2(${poMapper.code}Dto set${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.update${poMapper.code}By2(set${poMapper.code});
	}

	@Override
	public Integer read${poMapper.code}ByCodeNqIdForInteger(${poMapper.code}Dto read${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.select${poMapper.code}ByCodeNqIdForInteger(read${poMapper.code});
	}

	@Override
	public Integer read${poMapper.code}By2ForInteger(${poMapper.code}Dto read${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.select${poMapper.code}By2ForObject(Integer.class, read${poMapper.code}, null);
	}

	@Override
	public List<Integer> readList${poMapper.code}By1(${poMapper.code}Dto read${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.select${poMapper.code}By1ForObjectList(Integer.class, read${poMapper.code}, null,
				new String[] {});
	}

	@Override
	public List<Integer> readList${poMapper.code}By2(${poMapper.code}Dto read${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.select${poMapper.code}By2ForObjectList(Integer.class, read${poMapper.code}, null);
	}

	@Override
	public ${poMapper.code}Dto get${poMapper.code}By1(${poMapper.code}Dto get${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.select${poMapper.code}By1ForMappedObject(${poMapper.code}Dto.class, get${poMapper.code}, null, "id");
	}

	@Override
	public ${poMapper.code}Dto get${poMapper.code}ById(${poMapper.code}Dto get${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.select${poMapper.code}By1ForMappedObject(${poMapper.code}Dto.class, get${poMapper.code}, null, "id");
	}

	@Override
	public List<${poMapper.code}Dto> find${poMapper.code}By1(${poMapper.code}Dto find${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.select${poMapper.code}By1ForMappedObjectList(${poMapper.code}Dto.class, find${poMapper.code},
				new String[] {}, new String[] {});
	}

	@Override
	public List<${poMapper.code}Dto> find${poMapper.code}By2(${poMapper.code}Dto find${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.select${poMapper.code}By2ForMappedObjectList(${poMapper.code}Dto.class, find${poMapper.code});
	}

	@Override
	public PaginationDto<${poMapper.code}Dto> pagination${poMapper.code}By1For${poMapper.code}List(${poMapper.code}Dto pagination${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.pagination${poMapper.code}By1ForMappedObjectList(${poMapper.code}Dto.class,
				pagination${poMapper.code});
	}

	@Override
	public PaginationDto<Map<String, Object>> pagination${poMapper.code}By1ForMapList(${poMapper.code}Dto pagination${poMapper.code}) {
		return this.${businessCode?uncap_first}Dao.pagination${poMapper.code}By1ForMapList(pagination${poMapper.code});
	}
</#if>
</#list>
}